package com.huike.web.controller.system;

import com.huike.clues.service.ISysDeptService;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.TreeSelect;
import com.huike.common.core.domain.entity.SysDept;
import com.huike.common.utils.StringUtils;
import com.huike.web.result.RoleDeptTreeAjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Description TODO
 * @Author YEmeeyo
 * @Date 2023-07-11 19:39
 */
@RestController
@RequestMapping("/system/dept")
@Api(tags = "部门管理相关接口")
@Slf4j
public class SysDeptController extends BaseController{
    @Resource
    private ISysDeptService sysDeptService;


    /**
     * 查询部门列表（排除节点）
import com.huike.clues.domain.result.UserInfoAjaxResult;
import com.huike.clues.service.ISysDeptService;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.entity.SysDept;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "部门管理相关接口")
@Slf4j
@RestController
@RequestMapping("/system/dept")
public class SysDeptController extends BaseController {
    @Resource
    ISysDeptService iSysDeptService;

    @PostMapping()
    @ApiOperation("新增部门")
    public UserInfoAjaxResult insertDept(@RequestBody SysDept sysDept) {
        log.info("新增部门，操作人:{}", SecurityUtils.getUsername());
        iSysDeptService.insertDept(sysDept);
        return UserInfoAjaxResult.success();
    }

    @PutMapping()
    @ApiOperation("修改部门")
    public UserInfoAjaxResult updateDept(@RequestBody SysDept sysDept) {
        log.info("修改部门，操作人:{},操作对象id{}", SecurityUtils.getUsername(), sysDept.getDeptId());
        iSysDeptService.updateDept(sysDept);
        return UserInfoAjaxResult.success();
    }

    @GetMapping("/list")
    @ApiOperation("获取部门列表")
    public TableDataInfo listDept(SysDept sysDept) {
        log.info("获取部门列表，操作人:{}", SecurityUtils.getUsername());
        TableDataInfo tableDataInfo = iSysDeptService.listDept(sysDept);
        ;
        return tableDataInfo;
    }

    @Resource
    private ISysDeptService deptService;

    /**
     * 根据部门ID查询对应数据 回显
     *
     * @param deptId
     * @return
     */
    @GetMapping("/list/exclude/{deptId}")
    public AjaxResult selectExcludeDeptId(@PathVariable Long deptId) {

        //查询所有的部门
        List<SysDept> depts = sysDeptService.selectDeptList(new SysDept());

        //再遍历集合,若部门id和排除id相同就从集合里去除,这里要迭代器进行删除,不认会报空指针异常
        Iterator<SysDept> it = depts.iterator();
        while (it.hasNext()) {
            SysDept sd = (SysDept) it.next();
            if (sd.getDeptId().intValue() == deptId) {
                it.remove();
            }
        }
        return AjaxResult.success(depts);
    }

    /**
     * 获取对应角色部门列表树
     *
     * @param roleId
     * @return
     */
    @GetMapping("/roleDeptTreeselect/{roleId}")
    @ApiOperation("获取对应角色部门列表树")
    public RoleDeptTreeAjaxResult roleDeptTreeselect(@PathVariable Integer roleId) {

        List<Integer> deptIds = sysDeptService.selectByRoleId(roleId);

        List<SysDept> deptList = sysDeptService.selectByIds(deptIds);

        List<TreeSelect> treeSelectList = sysDeptService.buildDeptTreeSelect(deptList);
        RoleDeptTreeAjaxResult result = RoleDeptTreeAjaxResult.success();
        result.setCheckedKeys(deptIds);
        result.setDepts(treeSelectList);

        return result;
    }

    /**
     * 获取部门及人员
     *
     * @return
     */
    @GetMapping("/treeAnduser")
    public RoleDeptTreeAjaxResult treeAndUser() {

        return null;
    }
    @GetMapping("/{deptId}")
    @ApiOperation("根据部门ID查询对应数据")
    public AjaxResult findDeptByDeptId(@PathVariable Long deptId) {

        return AjaxResult.success(sysDeptService.findDeptByDeptId(deptId));
    }

    /**
     * 根据部门ID删除部门
     *
     * @param deptId
     * @return
     */
    @DeleteMapping("/{deptId}")
    @PreAuthorize("@ss.hasPermi('system:dept:remove')")
    @ApiOperation("根据部门ID删除部门")
    public AjaxResult deleteDeptByDeptId(@PathVariable Long deptId) {
        int rows = sysDeptService.deleteDeptByDeptId(deptId);

        //如果删除行数大于零,说明删除操作成功
        return toAjax(rows);
    }

    /**
     * 获取部门下拉树列表
     *
     * @param dept
     * @return
     */
    @GetMapping("/treeselect")
    @PreAuthorize("@ss.hasPermi('system:dept:query')")
    @ApiOperation("获取部门下拉树列表")
    public AjaxResult treeSelect(SysDept dept) {
        List<SysDept> depts = sysDeptService.treeSelect(dept);

        return AjaxResult.success(depts);
    }
}
